function Game(el) {
    this.t = null
    //保存地图对象
    this.el = document.querySelector(el)
    // 开始做地图
    this.map = new Map(this.el)//传递地图标签对象
    this.map.create()
    //搞食物
    this.food = new Food(this.el) //将地图传给食物构造函数，然后追加食物
    this.food.create()
    //搞小蛇
    this.snake = new Snake(this.el, this.t) //将地图传给小蛇构造函数，然后追加小蛇
    this.snake.create()
}
Game.prototype.start = function () {
    this.t = setInterval(() => {
        this.snake.move()
        if (this.snake.snakeObj[0].x < 0 || this.snake.snakeObj[0].x > 39) {
            this.len = 0
            clearInterval(this.t)
            alert('请刷新网页')
        }
        if (this.snake.snakeObj[0].y < 0 || this.snake.snakeObj[0].y > 19) {
            this.len = 0
            clearInterval(this.t)
            alert('请刷新网页')
        }
        // 接下来判断蛇是否吃到食物 原理：当蛇头坐标与事物的坐标一致的时候 食物消失 蛇长度加1
        let snakeX = this.snake.snakeObj[0].x * 20
        let snakeY = this.snake.snakeObj[0].y * 20
        let foodX = this.food.foodX
        let foodY = this.food.foodY
        if (snakeX == foodX && snakeY == foodY) {
            let last = this.snake.snakeObj[this.snake.snakeObj.length - 1]
            this.snake.snakeObj.push({
                x: last.x,
                y: last.y,
                color: last.color,
                ele: null
            })
            this.snake.create()
            this.food.move()
        }
    }, 200)
    document.addEventListener('keydown', (e) => {
        e = e || window.event
        //上下左右分别对应38 40 37 39
        switch (e.keyCode) {
            case 38:
                this.snake.direction = 'top'
                break;
            case 40:
                this.snake.direction = 'bottom'
                break;
            case 37:
                this.snake.direction = 'left'
                break;
            case 39:
                this.snake.direction = 'right'
                break;
            default:
                break;
        }
    })
}
Game.prototype.stop = function () {
    clearInterval(this.t)
}